/**
 * 发件箱功能
 * @author
 * @date 2012-11-20
 */

var colNames = ['', '收件人', '主题','内容','时间','','','',''];

/**
 * colModel 显示列的属性
 *
 * @type {Object}
 */
var colMode = [
	{ name:'first', index:'first', width:"100%", align:"center" },
	{ name:'draftReceiveNames',index:'draftReceiveNames', hidden:true }, 
	{ name:'title',index:'title', hidden:true }, 
	{ name:'content',index:'content', hidden:true }, 
	{ name:'formatCreateTime',index:'formatCreateTime', hidden:true }, 
	{ name:'senderName',index:'senderName', hidden:true }, 
	{ name:'senderId',index:'senderId', hidden:true },
	{ name:'draftReceiveIds',index:'draftReceiveIds', hidden:true },
	{ name:'id',index:'id', hidden:true },
];

/**
 * rowNum 想要显示的数据行数
 *
 * @type {Number}
 */

var rowNum = 15;

/**
 * total_last 用于判定表格的总页码是否发生了改变
 *
 * @type {Number}
 */

var total_last2 = 0;

/**
 * loadDataSuccess 判定是否成功加载完数据
 *
 * @type {Boolean}
 */

var loadDataSuccess2 = false;

$( "#draft-content" ).jqGrid({
	url:'../message/loaddraftData.html',
	datatype:"json",
	mtype: "POST",
	//data: local_data,
	height: "auto",
	autowidth: true,
	colNames: colNames, 
	colModel: colMode,
	rowNum: rowNum, 
	viewrecords: true,
	multiselect: true, 
	multiboxonly: true,
	emptyrecords : '没有邮件',
	pager: 'draftpager', 
	jsonReader : {
		root : "result",
		page : "page",
		total : "total",
		records : "records",
		repeatitems : false,
		cell : "cell",
		id : "id"
	},
	onSelectRow: function(rowid, status, e) {},
	ondblClickRow: function(rowid, iRow, iCol, event) {},
	gridComplete:function() {
		// 取消外边框
		$( "#gbox_draft-content" ).css( "border", "none" );
		
		// 隐藏表头
		$( "#gview_draft-content .ui-jqgrid-hdiv" ).css( "display", "none" );

		// 取消列间的分界限
		$( "#gview_draft-content .ui-jqgrid-bdiv td" ).css( "border-right-style", "none" );

		// 改变底边框样式
		$( "#gview_draft-content .ui-jqgrid-bdiv tr:not(:first-child) td" ).css( "border-bottom", "1px solid #E5E5E5" );
		
		// 取消最后一行的底边框
		$( "#gview_draft-content .ui-jqgrid-bdiv tr:last-child td" ).css( "border-bottom-style", "none" );
		
		// 设置表格行高
		//$( "#gview_draft-content .ui-jqgrid-bdiv tr:gt(0)" ).css( "height", "110px" );

		/**
		 * IDs 获取当前页所有数据的id
		 *
		 * @type {Object}
		 */

		var IDs = $( "#draft-content" ).jqGrid( "getDataIDs" );
		//没有数据
		if(IDs.length==0){
			$("#draftpager_center").hide();
			$(".ui-paging-info").css("text-align","left");
			$(".ui-paging-info").css("font-size","15px");
			$(".ui-jqgrid-view").css("height","60px");
		}
		
		for (var i=0; i < IDs.length; i++) {

			/**
			 * everyRowData 根据某一行数据的id获取该数据所有信息
			 *
			 * @type {Object}
			 */

			var everyRowData = $( "#draft-content" ).jqGrid( "getRowData", IDs[i] );
			
			// 收件箱信息
			metadata1 = '<div class="s-module">'
						+ '<div class="s-old-img"></div>'
						+ '<div class="sp-name">' + everyRowData.draftReceiveNames + '</div>'
					 	+ '<div class="sp-title">' + everyRowData.title + '</div>'
					 	+ '<div class="sp-content">' + everyRowData.content + '</div>'
					 	+ '<div class="t1">' + everyRowData.formatCreateTime + '</div>'
				 	+'</div>';

			$( "#draft-content" ).jqGrid( 'setRowData', IDs[i], { first: metadata1 } );

			// 取消提示信息
			$( ".metadata" ).parent("td").attr("title","");
			//点击返回进入草稿箱页
			$("#draft-back").click(function(){
				//详情页隐藏
				$(".s-detail").addClass("hide");
				$(".s-content").removeClass("hide");
			});

		}	
	},
	onSelectRow: function(rowid) {
		$(".s-content").addClass("hide");
		$(".s-detail").removeClass("hide");	
		//查看详情，加载详情信息
		var everyRowData = $( "#draft-content" ).jqGrid( "getRowData", rowid );
		//弹出发件人
		var senderName=everyRowData.senderName;
		var title =everyRowData.title;
		var content=everyRowData.content;
		document.getElementById("senderName").innerHTML=senderName;
		document.getElementById('formatCreateTime').innerHTML=everyRowData.formatCreateTime;
		document.getElementById('receiveName').innerHTML=everyRowData.draftReceiveNames;
		document.getElementById('title').innerHTML=title;
		$("#messageId").val(everyRowData.id);
		$("#content").val(content);
		$("#draftReceiveIds").val(everyRowData.draftReceiveIds);
	}
});
$("#draft-delete").click(function(){
	if (confirm("是否确认删除")){
	var id=$("#messageId").val();
	$.ajax({ 
		type: "POST",  
        url: "../message/deleteDraft.html",  
        data: "id="+id,  
        contentType : "application/x-www-form-urlencoded;charset=UTF-8",  
        success: function() {  
        	alert("删除成功！");
        	self.location="../message/index.html";
        },  
        error: function(err) {  
            alert(err);  
        }  
    });}else{
    	
    } 
});

$("#draft-send").click(function(){

	var draftReceiveIds=$("#draftReceiveIds").val();
	var receiveIds = draftReceiveIds.split("-");
	var title=$("#title").text();
	var content	=$("#content").val();
	var urlstr = "../message/";
	var datastr = "title="+title+"&content="+content;
	if(receiveIds.length==1){
		urlstr +="sendByOne.html";
		datastr +="&receiveId="+receiveIds[0];
	}
	else{
		urlstr +="sendByGroup.html";
		for(var i=0;i<receiverIds.length;i++){
			datastr +="&receiveIds="+receiveIds[i];
		}
	}
	$.ajax({ 
		type: "POST",  
		url : urlstr,
		data : datastr,  
        contentType : "application/x-www-form-urlencoded;charset=UTF-8",  
        success: function() {  
        	alert("发送成功！");
        	self.location="../message/index.html";
        },  
        error: function(err) {  
            alert(err);  
        }  
    }); 
	});